package com.gitee.wsgalaxy.gnodroid.module;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;

import com.gitee.wsgalaxy.gnodroid.GndGlobal;
import com.gitee.wsgalaxy.gnodroid.connect.GndMessage;

import org.json.JSONObject;

/**
 * Created by wsgalaxy on 4/14/18.
 */

public class GndDeviceInfoModule extends GndModule {
    public GndDeviceInfoModule()
    {
        module_id = "gnd_device_info_module";
    }

    public void dataRequest(byte[] bytes) {

        try {
            GndMessage gndMessage = GndMessage.generateMessage(bytes, bytes.length);
            if (gndMessage == null ||
                    gndMessage.message_data_list.size() == 0) {
                throw new Exception("generate message failure");
            }

            String strJson = new String(gndMessage.message_data_list.get(0).data, "UTF-8");
            JSONObject jsonObject = new JSONObject(strJson);

            if (jsonObject.getString("method").equals("get_device_info")) {
                //获取设备信息
                getDeviceInfo();
            } else {
                throw new Exception("no such method");
            }

        } catch (Exception e) {
            Log.d("gnd", module_id + ": " + e.getMessage());
            e.printStackTrace();

            String error_json = "{\"status\":2}";
            byte[] error_byte = null;
            try {
                error_byte = error_json.getBytes("UTF-8");
            } catch (Exception e2) {
            }

            GndMessage errorMessage = new GndMessage();
            errorMessage.addData(error_byte);
            try {
                session.dataSend(errorMessage.generateBytes());
            } catch (Exception e2) {
            }
        }
    }

    void getDeviceInfo() {
        try {

            JSONObject objectResult = new JSONObject();
            objectResult.put("status", GndGlobal.GND_JSON_STATUS_SUCCEED);


            TelephonyManager telephonyManager =
                    (TelephonyManager) GndGlobal
                            .context
                            .getSystemService(Context.TELEPHONY_SERVICE);
            if (PackageManager.PERMISSION_GRANTED ==
                    ContextCompat.checkSelfPermission(GndGlobal.context,
                            Manifest.permission.READ_PHONE_STATE)) {
                objectResult.put("imei", telephonyManager.getDeviceId());
            } else {
                objectResult.put("imei", GndGlobal.GND_MESSAGE_PERMISSION_DENY);
            }


            //弃用mac地址
            objectResult.put("mac", "useless");
            //**
            objectResult.put("cpu", "unknown");

            objectResult.put("android", "Android " + Build.VERSION.RELEASE);
            //**
            objectResult.put("kernel", "Linux " + Build.VERSION.BASE_OS);

            objectResult.put("sdk_api", "" + Build.VERSION.SDK_INT);

            objectResult.put("brand", Build.BRAND);

            objectResult.put("model", Build.MODEL);

            objectResult.put("product", Build.PRODUCT);

            objectResult.put("manufacturer", Build.MANUFACTURER);

            objectResult.put("cpu_abi", Build.CPU_ABI);

            DisplayMetrics metrics = new DisplayMetrics();
            GndGlobal.context.getResources().getDisplayMetrics();
            objectResult.put("pixel_x", "" + metrics.widthPixels);
            objectResult.put("pixel_y", "" + metrics.heightPixels);
            //**
            objectResult.put("ram", "" + 12000);
            //**
            objectResult.put("rom", "" + 23456);

            byte[] resultJsonByte = objectResult.toString().getBytes("UTF-8");
            //Byte[] resultJsonByte = resultJsonStr
            GndMessage resultMessage = new GndMessage();
            resultMessage.addData(resultJsonByte);

            session.dataSend(resultMessage.generateBytes());

        } catch (Exception e) {
            Log.d("gnd", module_id + ": " + e.getMessage());
            e.printStackTrace();

            String error_json = "{\"status\":2}";
            byte[] error_byte = null;
            try {
                error_byte = error_json.getBytes("UTF-8");
            } catch (Exception e2) {}
            GndMessage errorMessage = new GndMessage();
            errorMessage.addData(error_byte);
            try {
                session.dataSend(errorMessage.generateBytes());
            } catch (Exception e2) {}
            //session.dataSend(error_byte);
        }
    }
}
